#include "timespanlistwidget.h"
#include "mainwindow.h"
#include "addautoconnecttimespan.h"
#include "common.h"
#include <QContextMenuEvent>
#include <QMenu>
#include <QTime>

TimeSpanListWidget::TimeSpanListWidget(QWidget *parent):
    mParent(parent)
{


}


void TimeSpanListWidget::contextMenuEvent(QContextMenuEvent *event)
{
    QListWidgetItem* item=itemAt(event->pos());
    if(item!=nullptr&&item->text()!=QStringLiteral(AUTOTIMESPANHINT))
    {
        QMenu menu;
        QAction* a1=menu.addAction("修改");
        QAction* a2=menu.addAction("删除");
        QAction* artn=menu.exec(event->globalPos());
        if(artn==a1)
        {
            int i=-1;
            for(int j=0;j<count();j++)
            {
                if(item==this->item(j))
                {
                    i=j;
                    break;
                }
            }
            if(i!=-1)
            {
                AddAutoConnectTimeSpan w;
                MainWindow* p=static_cast<MainWindow*>(mParent->parentWidget());
                QStringList timespanlist= p->getAutoConntectTime();
                QStringList li=timespanlist.at(i).split("-");
                QTime t1=QTime::fromString(li[0],"hh:mm:ss");
                QTime t2=QTime::fromString(li[1],"hh:mm:ss");
                w.setTime(t1,t2);
                if(QDialog::Accepted==w.exec())
                {
                    timespanlist[i]=w.getRetstr();
                    p->setAutoConntectTime(timespanlist);
                    p->saveAutoConnectTime();
                    p->freshAutoConnectTime();
                }

            }

        }
        else if(artn==a2)
        {
            int i=-1;
            for(int j=0;j<count();j++)
            {
                if(item==this->item(j))
                {
                    i=j;
                    break;
                }
            }
            if(i!=-1)
            {
                emit(deleteItem(i));
            }

        }
    }
}

